using System;
using Castle.ActiveRecord;
using Castle.Components.Validator;
using NHibernate;
using NHibernate.Criterion;

namespace DecisionMaking.Domain
{
	[ActiveRecord]
	public class Account : ActiveRecordValidationBase<Account>
	{
		public Account ()
		{
		}	    
		
	    [PrimaryKey(PrimaryKeyType.Native)]
	    public int Id {set; get;}
		
		[Property(Length = 50), ValidateLength(2, 50)]
		public string FirstName {set; get;}
		
		[Property(Length = 50)]
		public string LastName {set; get;}

		[Property(UniqueKey = "uix_account_email", Update = false), ValidateNonEmpty]
		public string Email {set; get;}

		[Property]
		public string Password {set; get;}
		
		[Property(ColumnType = "StringClob")]
		public string Bio {set; get;}
		
		[Property]
		public bool SuperUser {set; get;}
		
		public static Account FindByEmail(string email)
		{
			return FindOne(DetachedCriteria
				.For(typeof(Account))
			    .Add(Restrictions.Eq("Email", email)));

		}
		
		public override bool Equals (object obj)
		{
			if (obj == null)
				return false;
			if (ReferenceEquals (this, obj))
				return true;
			if (obj.GetType () != typeof(Account))
				return false;
			DecisionMaking.Domain.Account other = (DecisionMaking.Domain.Account)obj;
			return Id == other.Id;
		}


		public override int GetHashCode ()
		{
			unchecked {
				return Id.GetHashCode ();
			}
		}

		
	}
}

